package com.guigu.liuzhireng.controller;

import com.guigu.liuzhireng.pojo.Accountrole;
import com.guigu.liuzhireng.pojo.Role;
import com.guigu.liuzhireng.service.AccountroleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("Accountrole.do")
public class AccountroleController {

    @Autowired
    AccountroleService accountroleService;

    @RequestMapping("AccountRoleAll")
    @ResponseBody
    public List<Role> AccountRoleAll(String account){
        return accountroleService.QueryAccountRole(account);
    }

    @RequestMapping("DelAdd")
    public String DelAdd(HttpServletRequest req){
        String accid = req.getParameter("accid");
        String[] roleids = req.getParameterValues("roleid");


        List<Accountrole> accountroles = new ArrayList<>();
        for (String role : roleids) {
            Accountrole accountrole = new Accountrole();
            accountrole.setRoleid(Integer.parseInt(role));
            accountrole.setAccid(accid);
            accountroles.add(accountrole);
        }
        accountroleService.DelAdd(accountroles,accid);
        return "/account/Account.html";
    }
}
